package com.rop.znyx.common.service;

import lombok.RequiredArgsConstructor;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * Created with IntelliJ IDEA
 * User: Rop
 * Date: 2023/7/3
 * Time: 15:26
 * Description:
 */

@Service
@RequiredArgsConstructor
public class RabbitService {

    //  引入操作rabbitmq 的模板
    private final RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     *
     * @param exchange   交换机
     * @param routingKey 路由键
     * @param message    消息
     */
    public boolean sendMessage(String exchange, String routingKey, Object message) {
        // 发送信息
        rabbitTemplate.convertAndSend(exchange, routingKey, message);
        return Boolean.TRUE;
    }
    //

    /**
     * 发送延迟消息
     *
     * @param exchange   交换机
     * @param routingKey 路由键
     * @param message    消息
     * @param delayTime  延迟时间
     */

    //
    //
    public boolean sendMessage(String exchange, String routingKey, Object message, int delayTime) {
        // 发送延迟信息
        rabbitTemplate.convertAndSend(exchange, routingKey, message, delayMessage -> {
            delayMessage.getMessageProperties().setDelay(delayTime * 1000);
            return delayMessage;
        });
        return Boolean.TRUE;
    }
}
